/**
Copyright (c) 2013, Andy Fox (andy.fox at qq.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "bsd_types.h"

#ifndef __BSD_SWAB_H__
#define __BSD_SWAB_H__

#define bsd_swab16(x) ((u16)(                   \
    (((u16)(x) & (u16)0x00ffU) << 8) |          \
    (((u16)(x) & (u16)0xff00U) >> 8)))

#define bsd_swab32(x) ((u32)(                   \
    (((u32)(x) & (u32)0x000000ffUL) << 24) |    \
    (((u32)(x) & (u32)0x0000ff00UL) <<  8) |    \
    (((u32)(x) & (u32)0x00ff0000UL) >>  8) |    \
    (((u32)(x) & (u32)0xff000000UL) >> 24)))

#define bsd_swahw32(x) ((u32)(                  \
    (((u32)(x) & (u32)0x0000ffffUL) << 16) |    \
    (((u32)(x) & (u32)0xffff0000UL) >> 16)))

#define bsd_swahb32(x) ((u32)(                  \
    (((u32)(x) & (u32)0x00ff00ffUL) << 8) |     \
    (((u32)(x) & (u32)0xff00ff00UL) >> 8)))

#if CPU_LITTLE_ENDIAN
#define bsd_htonl(x) ((be32)bsd_swab32((x)))
#define bsd_htons(x) ((be16)bsd_swab16((x)))
#define bsd_ntohl(x) bsd_swab32((be32)(x))
#define bsd_ntohs(x) bsd_swab16((be16)(x))

#define cpu_to_le32(x) ((le32)(u32)(x))
#define cpu_to_le16(x) ((le16)(u16)(x))
#define le32_to_cpu(x) ((u32)(le32)(x))
#define le16_to_cpu(x) ((u16)(le16)(x))

#define cpu_to_be32(x) ((be32)bsd_swab32((x)))
#define cpu_to_be16(x) ((be16)bsd_swab16((x)))
#define be32_to_cpu(x) bsd_swab32((u32)(be32)(x))
#define be16_to_cpu(x) bsd_swab16((u16)(be16)(x))
#elif CPU_BIG_ENDIAN
#define bsd_htonl(x) ((be32)(u32)(x))
#define bsd_htons(x) ((be16)(u16)(x))
#define bsd_ntohl(x) ((u32)(be32)(x))
#define bsd_ntohs(x) ((u16)(be16)(x))

#define cpu_to_le32(x) ((le32)bsd_swab32((x)))
#define cpu_to_le16(x) ((le16)bsd_swab16((x)))
#define le32_to_cpu(x) bsd_swab32((u32)(le32)(x))
#define le16_to_cpu(x) bsd_swab16((u16)(le16)(x))

#define cpu_to_be32(x) ((be32)(u32)(x))
#define cpu_to_be16(x) ((be16)(u16)(x))
#define be32_to_cpu(x) ((u32)(be32)(x))
#define be16_to_cpu(x) ((u16)(be16)(x))
#endif

#endif /* __BSD_SWAB_H__ */

